package com.lyw.common.valid.anno.validator;

import com.lyw.common.valid.anno.ListValues;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @Auther liyawei
 * @Date 2022/6/16 13:14
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValues,Integer> {

    private Set<Integer> set = new HashSet<>();

    // 初始化方法
    @Override
    public void initialize(ListValues constraintAnnotation) {

        // 获取我们制定规则的的值
        int[] values = constraintAnnotation.values();
        System.out.println(values.toString());
        for (int value : values) {
            set.add(value);
        }
    }

    /**
     * 判断是否校验成功
     * @param value 前端提交的值
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        // 判断是否包含当前值
        return set.contains(value);
    }
}
